package com.wuyan.locationdog.util.maputil

import android.graphics.Color
import android.os.Bundle
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.OnLifecycleEvent
import com.amap.api.maps.AMap
import com.amap.api.maps.CameraUpdateFactory
import com.amap.api.maps.MapView
import com.amap.api.maps.model.BitmapDescriptorFactory
import com.amap.api.maps.model.MyLocationStyle
import com.wuyan.locationdog.R


class MapLifecycle(private val mMapView: MapView, val savedInstanceState: Bundle?, private var isLocationEnabled: Boolean = true) :
    LifecycleObserver {


    init {
        mMapView.onCreate(savedInstanceState)
        initMap()
    }

    fun initMap() {
        val aMap = mMapView.map
        aMap.apply {
            mapType = AMap.MAP_TYPE_SATELLITE

            myLocationStyle = MyLocationStyle().apply {
                // 设置定位的类型为 跟随模式
                //https://lbs.amap.com/api/android-sdk/guide/create-map/mylocation?type=spa
                myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATION_ROTATE_NO_CENTER)
                // 自定义定位蓝点图标
                myLocationIcon(BitmapDescriptorFactory.fromResource(R.mipmap.mark_custom_loction))
                // 自定义精度范围的圆形边框颜色
                strokeColor(Color.alpha(0))
//                //自定义精度范围的圆形边框宽度
                strokeWidth(1f)
//                // 设置圆形的填充颜色
                radiusFillColor(Color.argb(0, 0, 0, 0))
                showMyLocation(true)
            }
            isMyLocationEnabled = isLocationEnabled// 设置为true表示显示定位层并可触发定位，false表示隐藏定位层并不可触发定位，默认是false
//         setOnMyLocationChangeListener(this)//设置SDK 自带定位消息监听

            uiSettings.apply {
                isMyLocationButtonEnabled = false//设置默认定位按钮是否显示，非必需设置。
                isZoomControlsEnabled = false//是否允许显示缩放按钮
                isRotateGesturesEnabled = false//不要旋转,防止mark角度和路径不对齐
            }
            moveCamera(CameraUpdateFactory.zoomTo(11f)) //设置缩放级别 地图的缩放级别一共分为 17 级，从 3 到 19。数字越大，展示的图面信息越精细。
        }
    }


    @OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
    fun onCreate() {
//        LogUtils.e("MapLifecycle---------onCreate()")
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
    fun onResume() {
        mMapView.onResume()
//        LogUtils.e("MapLifecycle---------onResume()")
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
    fun onPause() {
        mMapView.onPause()
//        LogUtils.e("MapLifecycle---------onPause()")
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
    fun onDestroy() {
        mMapView.onDestroy()
//        LogUtils.e("MapLifecycle---------onDestroy()")
    }

}